package com.tao.graduation.Controller;

import com.tao.graduation.Pojo.*;
import com.tao.graduation.Service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.print.DocFlavor;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //分页
    private final Integer LIMIT=5;

     private static  Map  update(Integer x){
        Map<String,Object> map=new HashMap<>();
        Integer count=x;
        if (count>0){
            map.put("message","修改成功!");
        }else {
            map.put("message","修改失败!");
        }
        return map;
    }


    private static Map delete(Integer del){
        Map<String,Object> map=new HashMap<>();
        Integer count=del;
        if (count>0){
            map.put("message","删除成功!");
        }else {
            map.put("message","删除失败!");
        }
        return map;
    }

    private static Map insert(Integer in){
        Map<String,Object> map=new HashMap<>();
        Integer count=in;
        if (count>0){
            map.put("message","上传成功!");
        }else {
            map.put("message","上传失败!");
        }
        return map;
    }


    @RequestMapping("/queryDoctor")
    public Map queryDoctor(@RequestBody Integer page){
        Integer index=LIMIT*(page-1);
        Map<String,Object> map=new HashMap<>();
        List<Doctor> list=adminService.queryDoctor(index,LIMIT);
        Integer count=adminService.queryDoctorCount();
        map.put("data",list);
        map.put("total",count);
        return map;
    }

    @PostMapping("/insertDoctorInfo")
    public Map insertDoctorInfo( Doctor doctor,@RequestParam("imgFile") MultipartFile imgFile) throws IOException {

        String photo=null;
        if (!imgFile.isEmpty()){
            System.out.println(imgFile.getSize());
            String originalFilename = imgFile.getOriginalFilename();
            imgFile.transferTo(new File("C:\\Users\\acer\\Desktop\\Graduation\\graduation\\src\\assets\\image\\"+originalFilename));
            photo=originalFilename;
        }

        Map<String,Object> map=new HashMap<>();
        Doctor doctor1=new Doctor(doctor.getName(),doctor.getAge(),doctor.getCertId()
                ,doctor.getSex(),doctor.getDepartment(),doctor.getAddress(),doctor.getExpert(),photo);
        Integer count=adminService.insertDoctorInfo(doctor1);
        if (count>0){
            map.put("message","上传成功!");
        }else {
            map.put("message","上传失败!");
        }
        return map;
    }

    @RequestMapping("/updateDoctor")
    public Map updateDoctor(Doctor doctor, MultipartFile imgFile) throws IOException {
        String photo=null;
        Integer count=0;
        Map<String, Object> map = new HashMap<>();
        if (imgFile!=null) {
            if (!imgFile.isEmpty()) {
                System.out.println(imgFile.getSize());
                String originalFilename = imgFile.getOriginalFilename();
                imgFile.transferTo(new File("C:\\Users\\acer\\Desktop\\Graduation\\graduation\\src\\assets\\image\\" + originalFilename));
                photo = originalFilename;
            }
            Doctor doctor1 = new Doctor(doctor.getId(), doctor.getName(), doctor.getAge(), doctor.getCertId()
                    , doctor.getSex(), doctor.getDepartment(), doctor.getAddress(), doctor.getExpert(), photo);
             count = adminService.updateDoctor(doctor1);
        }else {
            Doctor doctor1 = new Doctor(doctor.getId(), doctor.getName(), doctor.getAge(), doctor.getCertId()
                    , doctor.getSex(), doctor.getDepartment(), doctor.getAddress(), doctor.getExpert());
            count = adminService.updateDoctorHP(doctor1);
        }
        if (count>0){
            map.put("message","修改成功!");
        }else {
            map.put("message","修改失败!");
        }
        return map;
    }

    @RequestMapping("/deleteDoctor")
    public Map deleteDoctor(@RequestBody Integer id){
        Map<String,Object> map=new HashMap<>();
        Integer count=adminService.deleteDoctor(id);
        if (count>0){
            map.put("message","删除成功!");
        }else {
            map.put("message","删除失败!");
        }
        return map;
    }

    @RequestMapping("/searchDoctor")
    public Map searchDoctor( String name, String certId){
        Map<String,Object> map=new HashMap<>();
        List<Doctor> list=adminService.searchDoctor(name, certId);
        map.put("data",list);
        return map;
    }

    @RequestMapping("/queryPatient")
    public Map queryPatient(@RequestBody Integer page){
        Integer index=LIMIT*(page-1);
        Map<String,Object> map=new HashMap<>();
        List<Patient> list=adminService.queryPatient(index,LIMIT);
        map.put("data",list);
        map.put("total",adminService.queryPatientCount());
        return map;
    }

    @RequestMapping("/updatePatient")
    public Map updatePatient(@RequestBody Patient patient){
        Map<String,Object> map=new HashMap<>();
        Integer count=adminService.updatePatient(patient);
        if (count>0){
            map.put("message","修改成功!");
        }else {
            map.put("message","修改失败!");
        }
        return map;
    }

    @RequestMapping("/deletePatient")
    public Map deletePatient(@RequestBody Integer id){
        Map<String,Object> map=new HashMap<>();
        Integer count=adminService.deletePatient(id);
        if (count>0){
            map.put("message","删除失败!");
        }else {
            map.put("message","删除失败!");
        }
        return map;
    }

    @RequestMapping("/insertPatient")
    public Map insertPatient(@RequestBody Patient patient){
        Map<String,Object> map=new HashMap<>();
        Integer count=adminService.insertPatient(patient);
        if (count>0){
            map.put("message","上传成功!");
        }else {
            map.put("message","上传失败!");
        }
        return map;
    }

    @RequestMapping("/queryDrugs")
    public Map queryDrugs(@RequestBody Integer page){
        Integer index=LIMIT*(page-1);
        Map<String,Object> map=new HashMap<>();
        List<Drugs> list=adminService.queryDrugs(index,LIMIT);
        map.put("data",list);
        map.put("total",adminService.queryDrugsCount());
        return map;
    }

    @RequestMapping("/insertDrugs")
    public Map insertDrugs(@RequestBody Drugs drugs){
        Map<String,Object> map=new HashMap<>();
        Integer count=adminService.insertDrugs(drugs);
        if (count>0){
            map.put("message","上传成功!");
        }else {
            map.put("message","上传失败!");
        }
        return map;
    }

    @RequestMapping("/updateDrugs")
    public Map updateDrugs(@RequestBody Drugs drugs){
        Map<String,Object> map=new HashMap<>();
        Integer count=adminService.updateDrugs(drugs);
        if (count>0){
            map.put("message","上传成功!");
        }else {
            map.put("message","上传失败!");
        }
        return map;
    }

    @RequestMapping("/deleteDrugs")
    public  Map deleteDrugs(@RequestBody Integer id){
        Map<String,Object> map=new HashMap<>();
        Integer count=adminService.deleteDrugs(id);
        if (count>0){
            map.put("message","删除成功!");
        }else {
            map.put("message","删除失败!");
        }
        return map;
    }


    @RequestMapping("/searchDrugs")
    public Map searchDrugs(String name,Integer number){
        Map<String,Object> map=new HashMap<>();
        List<Drugs> list=adminService.searchDrugs(name, number);
        map.put("data",list);
        return map;
    }

    @RequestMapping("/queryOrder")
    public Map queryOrder(@RequestBody Integer page){
        Integer index=LIMIT*(page-1);
        Map<String,Object> map=new HashMap<>();
        List<Map<String,String>> list=adminService.queryOrder(index,LIMIT);
        map.put("data",list);
        map.put("total",adminService.queryOrderCount());
        return map;
    }

    @RequestMapping("/deleteOrder")
    public Map deleteOrder(@RequestBody Integer id){
        Map<String,Object> map=new HashMap<>();
        Integer count=adminService.deleteOrder(id);
        if (count>0){
            map.put("message","取消成功!");
        }else {
            map.put("message","取消失败!");
        }
        return map;
    }

    @RequestMapping("/queryNews")
    public Map queryNews(@RequestBody Integer page){
        Integer index=LIMIT*(page-1);
        Map<String,Object> map=new HashMap<>();
        List<News> list=adminService.queryNews(index,LIMIT);
        map.put("data",list);
        map.put("total",adminService.queryNewsCount());
        return map;
    }

    @RequestMapping("/insertNews")
    public Map insertNews(@RequestBody News news){
        return insert(adminService.insertNews(news));
    }

    @RequestMapping("/updateNews")
    public Map updateNews(@RequestBody News news){
        return update(adminService.updateNews(news));
    }

    @RequestMapping("/deleteNews")
    public Map deleteNews(@RequestBody Integer id){
         return delete(adminService.deleteNews(id));
    }

    @RequestMapping("/searchPatient")
    public Map searchPatient(String name,String certId){
        Map<String,Object> map=new HashMap<>();
        List<Patient> list=adminService.searchPatient(name,certId);
        map.put("data",list);
        return map;
    }

    @RequestMapping("/searchOrder")
    public Map searchOrder(String name,String doctorName){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,String>> list=adminService.searchOrder(name, doctorName);
        map.put("data",list);
        return map;
    }

    @RequestMapping("/searchNews")
    public Map searchNews(String title,String createTime){
        Map<String,Object> map=new HashMap<>();
        List<News> list=adminService.searchNews(title, createTime);
        map.put("data",list);
        return map;
    }

    @RequestMapping("/CountPeople")
    public Map CountPeople(@RequestBody Integer year){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Integer>> list=adminService.CountPeople(year);
        System.out.println(list);
        map.put("data",list);
        return map;
    }

    @RequestMapping("/CountCost")
    public Map CountCost(){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> list=adminService.CountCost();
        map.put("data",list);
        return map;
    }

    @RequestMapping("/queryHospital")
    public Map queryHospital(@RequestBody Integer page){
        Integer index=LIMIT*(page-1);
         Map<String,Object> map=new HashMap<>();
        List<Map<String,String>> list=adminService.queryHospital(index,LIMIT);
         map.put("data",list);
         map.put("total",adminService.queryHospitalCount());
         return map;
    }

    @RequestMapping("/updateHospital")
    public Map updateHospital(@RequestBody Integer id){
         Map<String,Object> map=new HashMap<>();
         Integer count=adminService.updateHospital(id);
         if (count>0){
             map.put("message","缴费成功!");
         }else {
             map.put("message","缴费失败!");
         }
         return map;
    }

    @RequestMapping("/queryResident")
    public Map queryResident(@RequestBody Integer page){
        Integer index=LIMIT*(page-1);
        Map<String,Object> map=new HashMap<>();
        List<Map<String,String>> list=adminService.queryResident(index,LIMIT);
        map.put("data",list);
        map.put("total",adminService.queryResidentCount());
        return map;
    }

    @RequestMapping("/queryScheduling")
    public Map queryScheduling(@RequestBody String option){
        Map<String,Object> map=new HashMap<>();
        List<Map<String,String>> list=adminService.queryScheduling(option);
        List<Integer> count=adminService.querySchedulingCount(option);
        map.put("data",list);
        map.put("name",adminService.querySchedulingName(option));
        map.put("count",count);
        return map;
    }

    @RequestMapping("/insertScheduling")
    public Map insertScheduling(@RequestBody Scheduling scheduling){
         return insert(adminService.insertScheduling(scheduling));
    }

    @RequestMapping("/deleteScheduling")
    public Map deleteScheduling(@RequestBody Integer id){
         return delete(adminService.deleteScheduling(id));
    }
}
